package com.gcxy.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author lichuandong
 * @Date 2022/2/16
 */
@Component
public class RedisTemplateUtil {

    @Autowired
    private RedisTemplate redisTemplate;


//    /**
//     * redis存入数据
//     * @param key 键名
//     * @param value  值
//     * @param time 保存时间
//     * @param timeUnit  时间单位
//     * */
//    public  void setValue(String key, Object value, int time, TimeUnit timeUnit) {
//        redisTemplate.opsForValue().set(key, value, time, timeUnit);
//    }

        public String getString(String key){
        Object o = redisTemplate.opsForValue().get(key);
        return (String) o;
    }

    public void setString(String key,String value){
        redisTemplate.opsForValue().set(key,value);
    }

    public List<String> getList(String key){
        List<String> range = redisTemplate.opsForList().range(key, 0, -1);
        return range;
    }

    public void setList(String key,List<String> list){
        redisTemplate.opsForList().leftPushAll(key,list);

    }

    public void remove(String key){
        redisTemplate.delete(key);
    }

    public void setHourExpire(String key,Integer time){
        redisTemplate.expire(key,time,TimeUnit.HOURS);
    }
    public void setMinuteExpire(String key,Integer time){
        redisTemplate.expire(key,time,TimeUnit.MINUTES);
    }
}
